package com.realphim.app;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

import com.realphim.app.model.ModelCategory;
import com.realphim.app.model.ModelPhim;
import com.realphim.app.util.CommonDefine;
import com.realphim.app.util.ConnectionDetector;
import com.realphim.app.util.JSONParser;
import com.realphim.app.util.MyApplication;
import com.realphim.app.util.MyCache;
import com.realphim.app.util.CommonDefine.KeyPhim;

public class ActivitySplash extends Activity implements CommonDefine {

    private final String TAG = "ActivitySplash";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        setContentView(R.layout.activity_splash);
        MyApplication myApp = (MyApplication) getApplication();
        myApp.customAppMethod();
        MyCache.getInstance().customSingletonMethod();

        if (ConnectionDetector.isConnectingToInternet(this)) {
            new AsyncTaskGetListFeature()
                    .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {
            showDialogNoConnection();
        }
    }

    private class AsyncTaskGetListFeature extends
            AsyncTask<String, String, Boolean> {

        @Override
        protected Boolean doInBackground(String... params) {
            boolean result = false;
            try {

                String strUrl = SERVICE_GET_LIST_FILM + ServiceParam.IS_FEATURE
                        + "=1&" + ServiceParam.LIMIT + "=5";
                Log.v(TAG, strUrl);
                String jsonResult = new JSONParser().getJSONFromUrl(strUrl,
                        METHOD_GET, null);
                Log.i(TAG, jsonResult);
                JSONArray arrResult = new JSONObject(jsonResult)
                        .getJSONArray("data");
                for (int i = 0; i < arrResult.length(); i++) {
                    JSONObject objPhim = arrResult.getJSONObject(i);
                    ModelPhim myPhim = new ModelPhim();
                    myPhim.id = objPhim.getInt(KeyPhim.id);
                    myPhim.title = objPhim.getString(KeyPhim.title);
                    myPhim.titleTranslate = objPhim
                            .getString(KeyPhim.title_translate);
                    myPhim.imageUrl = objPhim.getString(KeyPhim.image);
                    myPhim.thumbnailUrl = objPhim.getString(KeyPhim.thumbnail);
                    myPhim.description = objPhim.getString(KeyPhim.description);
                    myPhim.lastEp = objPhim.getInt(KeyPhim.last_episode);
                    myPhim.maxEp = objPhim.getInt(KeyPhim.max_episode);
                    myPhim.isHD = objPhim.getInt(KeyPhim.is_hd);
                    myPhim.imb = objPhim.getString(KeyPhim.imdb);
                    myPhim.publishYear = objPhim
                            .getString(KeyPhim.publish_year);
                    myPhim.duration = objPhim.getInt(KeyPhim.duration);
                    myPhim.isFeature = objPhim.getInt(KeyPhim.is_featured);
                    myPhim.isCinema = objPhim.getInt(KeyPhim.is_cinema);
                    myPhim.rate = objPhim.getString(KeyPhim.rate);
                    myPhim.director = objPhim.getString(KeyPhim.director);
                    myPhim.viewFake = objPhim.getString(KeyPhim.view_fake);
                    myPhim.label = objPhim.getString(KeyPhim.label);
                    myPhim.actors = objPhim.getString(KeyPhim.actors);
                    myPhim.nation = objPhim.getString(KeyPhim.nation);
                    myPhim.tag = objPhim.getString(KeyPhim.tags);
                    myPhim.uploadBy = objPhim.getString(KeyPhim.upload_by);
                    myPhim.creatDate = objPhim.getString(KeyPhim.created_date);
                    myPhim.viewCount = objPhim.getString(KeyPhim.view_count);
                    myPhim.viewCountMonth = objPhim
                            .getString(KeyPhim.view_count_month);
                    myPhim.viewCountWeek = objPhim
                            .getString(KeyPhim.view_count_week);

                    JSONArray arrCategory = objPhim
                            .getJSONArray(KeyPhim.category);
                    for (int j = 0; j < arrCategory.length(); j++) {
                        JSONObject objCate = arrCategory.getJSONObject(j);
                        ModelCategory myCate = new ModelCategory();
                        myCate.id = objCate.getInt(KeyCategory.id);
                        myCate.title = objCate.getString(KeyCategory.title);
                        myPhim.listCategories.add(myCate);
                    }

                    MyCache.getInstance().listPhimFeature.add(myPhim);
                    result = true;

                }
            } catch (Exception e) {
                Log.e(TAG, e.toString() + "");
                result = false;
            }
            return result;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (result) {
                Intent i = new Intent(ActivitySplash.this, ActivityMain.class);
                startActivity(i);
                finish();
            } else {
                showDialogNoConnection();
            }
        }

    }

    private void showDialogNoConnection() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(
                ActivitySplash.this);
        dialog.setTitle("Connection");
        dialog.setMessage("Can not connect to server");
        dialog.setPositiveButton("OK", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        });
        dialog.show();
    }

}
